<?php
// DB Created by Rich L. a member in service in Northern Delaware.  Please feel free to share this code wherever it may be useful to alcholoics.  Should you have questons please contact me at sobernerd@gmail.com
//header items
$jQuery = true;
$tinyMCE = false;
$jQueryCookie = true;
$hoverIntent = false;
$Jededitable = false;
$sectionSort = false;
$googleAPI = false;
$calendar = false;
$pageTitle = 'Soberpress - Administration - Meeting List';
require_once ('../../config/include_paths.php');
require_once('config.php');
require_once('functions.php');
require_once('dbconnect.php');	// database connect script.
require_once('tcpdf.php');
$sum_query = mysql_query('SELECT * from meetings');
$sum_meetings = mysql_num_rows($sum_query);
$date= date("F d, Y");
$query = 	'SELECT meetings.id AS meeting_id, day, mtime, city, name, group_id, groupname, code, open_closed, men_women, hc 
			 FROM meetings,groups,locations,meeting_types 
			 WHERE group_id=groups.id and location_id=locations.id and mtg_type_id=meeting_types.id ORDER BY day, mtime, city ASC';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$dayheader = 0;

$data = array();

while ($row = mysql_fetch_array($result))
{
if($row[ir_fname]){$name="$row[ir_fname] $row[ir_lname]";}else{$name="";}

if ($dayheader != $row[day])
{
// Set $longday
	switch ($row[day])
	{
		case 1:
			$longday="Sunday \n";
			break;
		case 2:
			$longday="Monday \n";
			break;
		case 3:
			$longday="Tuesday \n";
			break;
		case 4:
			$longday="Wednesday \n";
			break;
		case 5:
			$longday="Thursday \n";
			break;
		case 6:
			$longday="Friday \n";
			break;
		case 7:
			$longday="Saturday \n";
			break;
	}
}else{
$longday="";}
$dayheader = $row[day];
$mtime=$row[mtime];
$rawtime = strtotime($mtime);	
$meetings_time = date('g:iA',$rawtime);	
}
// extend TCPF with custom functions
class MYPDF extends TCPDF {
	//Page header
    public function Header() {
        // Set font
		// Position at 15 mm from top and left
        $this->SetFont('helvetica', 'B', 20);
        // Title
        $this->Cell(0, 15, 'Meeting List', 0, false, 'C', 0, '', 0, false, 'M', 'M');
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
/*$pdf->ezColumnsStart(array('num'=>3));

$pdf->ezSetMargins(10,10,10,10);	
$table[] = array("Day" =>"$longday"."\n",
"Time" =>"$meetings_time",
"Description" =>$row["groupname"]."\n".$row["name"]."\n".$row["open_closed"].' '.$row["code"]);
}

$pdf->ezTable($table, "", "",
array('width'=>250,'xOrientation'=>'left', 'xPos'=>265, 'showHeadings'=>0, 'showLines'=>0, 'shaded'=>0));
$pdf->ezColumnsStop();
$pdf->ezStream();*/
// Colored table
	public function ColoredTable($header,$output) {
		// Colors, line width and bold font
		$this->SetFillColor(255, 0, 0);
		$this->SetTextColor(255);
		$this->SetDrawColor(128, 0, 0);
		$this->SetLineWidth(0.3);
		$this->SetFont('', 'B');
		// Header
		$w = array(5, 80, 90, 35, 15, 35);
		$num_headers = count($header);
		for($i = 0; $i < $num_headers; ++$i) {
			$this->Cell($w[$i], 6, $header[$i], 1, 0, 'C', 1);
		}
		$this->Ln();
		// Color and font restoration
		$this->SetFillColor(224, 235, 255);
		$this->SetTextColor(0);
		$this->SetFont('');
		// Data
		$fill = 0;
		foreach($output as $row) {
			$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
			$this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
			$this->Cell($w[2], 6, $row[2], 'LR', 0, 'L', $fill);
			$this->Cell($w[3], 6, $row[3], 'LR', 0, 'L', $fill);
			$this->Cell($w[4], 6, $row[4], 'LR', 0, 'M', $fill);
			$this->Cell($w[5], 6, $row[5], 'LR', 0, 'L', $fill);
			$this->Ln();
			$fill=!$fill;
		}
		$this->Cell(array_sum($w), 0, '', 'T');
		$this->Ln();
	}
}

// create new PDF document
$pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Soberpress');
$pdf->SetTitle($orgname.' - Role Call');
$pdf->SetSubject('Role call for representative meetings');
$pdf->SetKeywords('Role Call, Groups, Intergroup');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 20, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 10);

// add a page
$pdf->AddPage();

//Column titles
$header = array('X', 'Group', 'Location', 'City', 'State', 'Intergroup Rep');

//Data loading
//use $data array already created

// print colored table
$length = 28; //number on lines per psge
$num_pages = count($data)/$length;
$offset = 0;
$i = 0; 

	while($i < $num_pages) {
		$output = array_slice($data, $offset, $length);
		$pdf->ColoredTable($header,$output);
		$offset = $offset + $length;
		++$i;
		if ($i < $num_pages){
		$pdf->AddPage();  // add page at end
		}
	}


// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('meeting_list.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+

?>